﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;
using System.Data;


namespace RH.EBS.Core.Repository.DBUtility
{
    [Serializable]
    public class SqlItem : ISerializable
    {
        List<SqlParameters> m_parameterList = new List<SqlParameters>();
        string m_strSql = "";
        bool m_isProc = false;
        public SqlItem()
        {
        }

        public string StrSql
        {
            set
            {
                m_strSql = value;
            }
            get
            {
                return m_strSql;
            }
        }
        public List<SqlParameters> ParameterList
        {
            get
            {
                return m_parameterList;
            }
        }
        /// <summary>
        /// 是否是存储过程
        /// </summary>
        public bool IsProc
        {
            set
            {
                m_isProc = value;
            }
            get
            {
                return m_isProc;
            }
        }
        public void AppendParameter(SqlParameters param)
        {
            m_parameterList.Add(param);
        }

        public void AppendParameter(string name, object value, Type type)
        {
            m_parameterList.Add(new SqlParameters(name, value, type));
        }

        public void AppendParameter(string name, string value)
        {
            m_parameterList.Add(new SqlParameters(name, value, typeof(string)));
        }

        protected SqlItem(SerializationInfo info, StreamingContext context)
        {
            m_strSql = info.GetString("m_strSql");
            int parameter_count = info.GetInt32("parameter_count");
            for (int i = 0; i < parameter_count; i++)
            {
                m_parameterList.Add((SqlParameters)info.GetValue(i.ToString(), typeof(SqlParameters)));
            }
        }

        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("m_strSql", m_strSql);
            info.AddValue("parameter_count", m_parameterList.Count);
            for (int i = 0; i < m_parameterList.Count; i++)
            {
                info.AddValue(i.ToString(), m_parameterList[i], typeof(SqlParameters));
            }
        }
    }
}
